package com.example.shop.home

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.BannerEntityResult
import com.example.base.entity.CategoryEntityResult
import com.example.base.entity.GoodsEntityResult
import com.example.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class HomeViewModel:BaseViewModel() {
    val bannerSuccess = MutableLiveData<BannerEntityResult>()
    val bannerFail = MutableLiveData<String>()
    fun banner(){
        viewModelScope.launch {
            try {
                val result = apiServer.banner()
                bannerSuccess.value = result
            }catch (e:Exception){
                bannerFail.value = e.message
            }
        }
    }
    val categorySuccess = MutableLiveData<CategoryEntityResult>()
    val categoryFail = MutableLiveData<String>()
    fun category(){
        viewModelScope.launch {
            try {
                val result = apiServer.category()
                categorySuccess.value = result
            }catch (e:Exception){
                categoryFail.value = e.message
            }
        }
    }
    val goodsSuccess = MutableLiveData<GoodsEntityResult>()
    val goodsFail = MutableLiveData<String>()
    fun goods(categroyId:Int,currentPage:Int,pageSize:Int){
        viewModelScope.launch {
            try {
                val result = apiServer.goods(categroyId,currentPage, pageSize)
                goodsSuccess.value = result
            }catch (e:Exception){
                goodsFail.value = e.message
            }
        }
    }
}